package bTree.LeetCode;

public class 删除链表的倒数第N个结点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head.next==null){
            return null;
        }
        int sum=0;
        ListNode p=head;
        while (p!=null){
            sum++;
            p=p.next;
        }
        int target=sum-n+1;
        ListNode v=new ListNode();
        v.next=head;
        ListNode left=v;
        ListNode right=head;
        int cnt=0;
        while (cnt!=target){
            cnt++;
            if (cnt==target){
                left.next=right.next;
                return v.next;
            }
            left=right;
            right=right.next;
        }
        return null;
    }
}